﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvRide.Core.ObjectModel;
using NUnit.Framework;
using log4net.Core;
using CodeMadeSimple.Common.Tests;
using System.IO;

namespace EnvRide.Core.UnitTests
{
    /// <summary>
    /// Base class for EnvRide tests.
    /// </summary>
    public class EnvRideBaseTest : LoggedBaseTest
    {
        /// <summary>
        /// Asserts that there are no errors on the supplied Arguments and no errors logged events on this instance appender.
        /// </summary>
        /// <param name="args">Arguments to assert.</param>
        public void AssertHasNoErrors(EnvRideProcessArguments args)
        {
            Assert.IsFalse(args.HasErrors, "EnvRideProcessArguments instance should not have errors!");

            AssertDoesNotContainsLoggedMessages(Level.Error);
        }

        /// <summary>
        /// Asserts that there are errors on the supplied Arguments and that this instance appender contains the supplied error messages.
        /// </summary>
        /// <param name="args">Arguments to assert.</param>
        /// <param name="messages">Messages that should exist.</param>
        public void AssertHasErrors(EnvRideProcessArguments args, object[] messages)
        {
            Assert.IsTrue(args.HasErrors, "EnvRideProcessArguments instance should have errors!");

            AssertContainsLoggedMessages(Level.Error, messages);
        }

        /// <summary>
        /// Returns the error message for xpath expressions that does not find nodes.
        /// </summary>
        /// <param name="xPath"></param>
        /// <returns></returns>
        public string GetXPathDidNotReturnNodesMessage(string xPath)
        {
            return String.Format("The XPath \"{0}\" did not return any nodes!", xPath);
        }

        protected string GetPropertyContainerNullMessage()
        {
            return "There are properties references but the PropertyContainer property its null!";
        }

        protected String GetOverrideNotFoundMessage(string overName)
        {
            return String.Format("The override \"{0}\" was not found!", overName);
        }

        protected String GetEnvironmentNotFoundMessage(string envName, OverrideInclude include, EnvRideProcessArguments args)
        {
            String filePath = args.ParseValue(include.FilePath);

            if (!Path.IsPathRooted(filePath))
                filePath = Path.GetFullPath(Path.Combine(include.Project.BaseDirectory, filePath));

            return String.Format("The environment \"{0}\" was not found at project \"{1}\"!", envName, filePath);
        }
    }
}
